import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Main from '../components/blog/main.vue'
import Welcome from '../components/welcome.vue'
import Blog from '../components/blog/blog.vue'
import InsertBlog from '../components/blog/insertblog.vue'
import Index from '../components/h/index.vue'
import HMain from '../components/h/h_main.vue'
import HSort from '../components/h/h_sort.vue'
import HTag from '../components/h/h_tag.vue'
import HTime from '../components/h/h_time.vue'
import HDetail from '../components/h/h_detail.vue'
import HAboutUs from '../components/h/h_abus.vue'
import Tags from '../components/blog/tags.vue'
import InsertTag from '../components/blog/insertTag.vue'
import Sorts from '../components/blog/sorts.vue'
import InsertSort from '../components/blog/insertSort.vue'
import Comments from '../components/blog/comments.vue'
import UserInfo from '../components/blog/userInfo.vue'
import ChangeUser from "../components/blog/changeUser.vue"
import HSearch from '../components/h/h_search.vue'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: "/h_index" },
  { path: '/login', component: Login },
  {
    path: '/main', component: Main, redirect: '/welcome', children: [
      { path: '/insertblog', component: InsertBlog },
      { path: '/welcome', component: Welcome },
      { path: '/blog', component: Blog },
      { path: '/tag', component: Tags },
      { path: '/insertTag', component: InsertTag },
      { path: '/sorts', component: Sorts },
      { path: '/insertSort', component: InsertSort },
      { path: '/comments', component: Comments },
      {path: '/userInfo', component: UserInfo},
      {path: '/changeUser', component: ChangeUser},
    ]
  },
  {
    path: '/h_index', component: Index, redirect: '/h_main', children: [
      { path: '/h_main', component: HMain },
      { path: '/h_sort/:id', component: HSort },
      { path: '/h_tag/:id', component: HTag },
      { path: '/h_time', component: HTime },
      { path: '/h_detail/:id', component: HDetail },
      { path: '/h_abus', component: HAboutUs },
      { path: '/h_search', component: HSearch }
    ]
  }

]

const router = new VueRouter({
  mode: 'history',
  routes
})
//白名单
const whiteRouter=['/','/h_index','/h_main','/h_sort','/h_tag','/h_time','/h_detail','/h_abus','/login','/h_search']
router.beforeEach((to,from,next)=>{
 
  var d= window.sessionStorage.getItem("token")
  if(d){
    return next();
  }else{
    if(whiteRouter.indexOf(to.path)!==-1||whiteRouter.indexOf(to.path.substring(0,to.path.lastIndexOf("/")))!==-1){
      next();
    }else{
      next("/login")
    }
  }
  
})

export default router
